package com.tm.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tm.dto.DishDTOD;
import com.tm.dto.SetmealDTO;
import com.tm.dto.SetmealDTOO;
import com.tm.entity.DishEntity;
import com.tm.entity.SetmealEntity;
import com.tm.service.SetmealService;
import com.tm.vo.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangp
 * @create 2023-04-06 15:59
 */
@RestController
@RequestMapping("setmeal")
@CrossOrigin
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @GetMapping("page")
    public Result getSetmealPage(@RequestParam(required = false,defaultValue = "1") Integer page,
                                 @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                                 SetmealDTO setmealDTO){

        Page<SetmealEntity> page1 = setmealService.pageList(page,pageSize,setmealDTO);
        return Result.success(page1);
    }

    @PostMapping
    public Result addSetmeal(@RequestBody SetmealDTOO SetmealDTOO){
        setmealService.addSetmeal(SetmealDTOO);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result querySetmealById(@PathVariable("id") Long id){

        SetmealDTOO setmealDTOO = setmealService.getByIfwithDish(id);

        return Result.success(setmealDTOO);

    }

    @DeleteMapping
    public Result batchDelete(@RequestParam("ids") List<Long> ids){

        log.info("ids:{}",ids);
        setmealService.removeWithDish(ids);
        return Result.success("套餐数据删除成功");
    }

   /* @PutMapping
    public Result editDish(@RequestBody SetmealDTOO SetmealDTOO){

        //UpdateWrapper<SetmealEntity> updateWrapper = new UpdateWrapper<>();
        //updateWrapper.eq("id",setmealEntity.getId());
        Boolean flag = setmealService.updateById(SetmealDTOO);
        return Result.success(flag);

    }*/

    @PutMapping
    public Result editDish(@RequestBody SetmealDTOO SetmealDTOO) {

        setmealService.updateWithSetmeal(SetmealDTOO);

        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation(value = "批量修改菜品状态接口")
    public Result dishStatusByStatus(@PathVariable Integer status,@RequestParam List<Long> ids){
        setmealService.setmealStatusByStatus(status,ids);
        return Result.success();
    }

}
